package com.epam.lunch.domain;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.epam.lunch.utils.ExcludeFromToString;

public class BaseDomain {

	@Override
	public String toString() {
		StringBuffer ret = new StringBuffer();

		Class<? extends BaseDomain> clazz = this.getClass();
		ret.append(clazz.getSimpleName());
		ret.append("[");

		Field[] declaredFields = clazz.getDeclaredFields();
		for (Field field : declaredFields) {

			boolean annotationPresent = field.isAnnotationPresent(ExcludeFromToString.class);
			if (!annotationPresent || field.getAnnotation(ExcludeFromToString.class).notReally()) {
				String fieldName = field.getName();

				String getterName = "get"
						+ fieldName.substring(0, 1).toUpperCase()
						+ fieldName.substring(1);
				try {
					Method method = clazz.getMethod(getterName);
					Object value = method.invoke(this);

					ret.append(fieldName + "=" + value + ", ");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		ret.append("]");
		return ret.toString();
	}

}
